package com.tao.mytestgreendao

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.tao.mytestgreendao.gen.FundsinfoBeanDao
import com.tao.mytestgreendao.gen.UserEntityDao
import com.tao.mytestsocket.R
import kotlinx.android.synthetic.main.activity_test_grendao.*
import java.io.File

class TestGreenDaoActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test_grendao)
        DBNames.userDbName = "${getExternalFilesDir("db")?.absolutePath}${File.separator}user.db"
        create(window.decorView)
    }

    fun create(view: View) {
        val currentTimeMillis = System.currentTimeMillis()
        MyDaoManager.daoManager.init(this)
        tv_create_time.setText("耗时：${System.currentTimeMillis() - currentTimeMillis}")
    }

    fun add(view: View) {
        val currentTimeMillis = System.currentTimeMillis()

       val list = mutableListOf<FundsinfoBean>()
        for (i in 0..1000){
            val element = FundsinfoBean("510300" ,"2022-10-22 12:00:15","12."+i,"13.56"+i,"286.323"+i,"1452.5"+i,"13.245"+i,"114564"+i)
            list.add(element)
        }
        
        val fundsInfoDao = MyDaoManager.daoManager.getFundsInfoDao()
        fundsInfoDao?.insertInTx(list)

        tv_add_time.setText("耗时：${System.currentTimeMillis() - currentTimeMillis}")

    }

    fun change(view: View) {
        val userEntityDao = MyDaoManager.daoManager.getUserDao()

        val list =
            userEntityDao?.queryBuilder()?.where(UserEntityDao.Properties.Id.ge(0))?.build()?.list()
        val currentTimeMillis = System.currentTimeMillis()
        val get = list?.get(0)
//        userEntityDao?.update(get.also {
//            it!!.age=100
//        })
        UserEntityUpdateWraper(userEntityDao, arrayOf(UserEntityDao.Properties.Id)).updateByWhere(UserEntity().also { 
            it.age=1
            it.name=get?.name}, arrayOf(UserEntityDao.Properties.Sex))

        tv_change_time.setText("耗时：${System.currentTimeMillis() - currentTimeMillis}")

    }

    fun delete(view: View) {
        val userEntityDao = MyDaoManager.daoManager.getUserDao()

        val list =
            userEntityDao?.queryBuilder()?.where(UserEntityDao.Properties.Id.ge(0))?.build()?.list()

        val currentTimeMillis = System.currentTimeMillis()

        userEntityDao?.delete(list?.get(0))

        tv_delete_time.setText("耗时：${System.currentTimeMillis() - currentTimeMillis}")
    }

    fun query(view: View) {

 
        val currentTimeMillis = System.currentTimeMillis()

        val list = MyDaoManager.daoManager.getFundsInfoDao()?.queryBuilder()?.where(FundsinfoBeanDao.Properties.Id.ge(0))?.build()?.list()

        tv_query_time.setText("耗时：${System.currentTimeMillis() - currentTimeMillis} size" + list?.size)

    }

    fun query_where(view: View) {
        val currentTimeMillis = System.currentTimeMillis()

        val list =   MyDaoManager.daoManager.getFundsInfoDao()?.queryBuilder()
            ?.where(FundsinfoBeanDao.Properties.Id.eq(1000))?.list()

        tv_query_time.setText("耗时：${System.currentTimeMillis() - currentTimeMillis} size" + list?.size)

    }
}